package com.qianzhou.dao;

import com.qianzhou.io.Resources;
import com.qianzhou.pojo.User;
import com.qianzhou.sqlSession.SqlSession;
import com.qianzhou.sqlSession.SqlSessionFactory;
import com.qianzhou.sqlSession.SqlSessionFactoryBuild;

import java.io.InputStream;
import java.util.List;

/**
 * @ClassName IUserDaoimpl
 * @Description TODO 1. dao的实现类中存在重复的代码，整个操作的过程模板重复(创建sqlsession,调用sqlsession方 法，关闭 sqlsession)
 * 2。dao的实现类中存在硬编码，调用sqlsession的方法时，参数statement的id硬编码  -- 通过代理设计模型  来解决上面的两个问题
 * @Author zhouqian
 * @Date 2022/1/16 2:32 PM
 * @Version 1.0
 */

public class IUserDaoImpl implements IUserDao{
    public List<User> selectList() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryBuild.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("com.qianzhou.dao.IUserDao.selectList", null);
        return users;
    }

    public User selectOne(User user) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("UserMapper.xml");
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryBuild.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user1 = sqlSession.selectOne("com.qianzhou.dao.IUserDao.selectOne", user);
        System.out.println(user1);
        return user1;
    }
}
